import config from './config.js';
import axios from "axios";
import { UniAdapter } from "uniapp-axios-adapter";
import fun from './function.js';
// import {encryptedData, decryptedData} from '../common/encryp.js'
// import {router,RouterMount} from '../router.js'  //路径换成自己的
// import JSONBIG from 'json-bigint'

let encryp = true  //加密
let that = this

export default {
	request(options = {}) {
		return new Promise((resolve, reject) => {
			//允许 ajax 请求时跨域带cookie   因为H5项目中使用了 session ，是通过设置cookie 来实现的
			options.withCredentials=true
			let url = options.url;
			
			if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
				options.url = config.domain + url;
			}
			let token = uni.getStorageSync('token');
			// 设置自定义头部content-type
			options.header = {
				'content-type': 'application/json;charset=UTF-8',
				
			}
			if(token){
			  options.header.token =  token
			  
			  // options.header.token = token
			}
			options.complete = (response) => {
				if (response.data.code == -2) {
					uni.removeStorageSync("token")
					uni.removeStorageSync("hasLogin")
					uni.navigateTo({
						url: `/pages/login/login`
					})
				}
				// console.log(response,'response111111')
				if(response.data.code == 503){
					uni.showToast({
						title: response.data.msg,
						icon: "none"
					});
				}
				if (response.statusCode == 200) {
					
					resolve(response.data)
				} else {
					uni.showToast({
						title: '请求异常！',
						icon: "none"
					});
				}
			}
			options.fail = (err)=> {
			        uni.hideLoading();
			        uni.showModal({
			            title: '提示',
			            showCancel: false,
			            content: '当前人数访问过多，请稍后再试'
			        })
			}
			if(options.data && encryp){
			  console.log(options.data)
			}
			uni.request(options)
		})
	},
	requestFormData(options = {}){

		return new Promise((resolve, reject) => {
			uni.showLoading();
			//允许 ajax 请求时跨域带cookie   因为H5项目中使用了 session ，是通过设置cookie 来实现的
			options.withCredentials=true
			let url = options.url;
			// console.log(config)
			if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
				options.url = config.domain + url;
			}
			let formData = new FormData;
			Object.keys(options.data).forEach(key => {
				formData.append(key,options.data[key]);
			});
			let xhr = new XMLHttpRequest();
			xhr.open('post',options.url);
			// xhr.setRequestHeader('content-type','application/json;charset=UTF-8')
			xhr.withCredentials = true;
			xhr.onload = function () {
				uni.hideLoading();
				if (xhr.status >= 200 && xhr.status < 300) {
					// 请求成功，处理返回的数据
					let msg = JSON.parse(xhr.responseText);
					let response = {data:msg};
					resolve(JSON.parse(xhr.responseText));
					if (response.data.code == -2) {
						// uni.removeStorageSync("token")
						uni.removeStorageSync("hasLogin")
						uni.navigateTo({
							url: '/pages/login/login'
						})
						return;
					}
					// console.log(response,'response111111')
					if(response.data.code == 503){
						uni.showToast({
							title: response.data.msg,
							icon: "none"
						});
						return;
					}
					resolve(response.data)
				} else {
					// 请求失败
					uni.showToast({
						title: '请求异常！',
						icon: "none"
					});
					reject(xhr);
				}
			};

			// 设置请求发生错误时的回调函数
			xhr.onerror = function () {
				uni.hideLoading();
				uni.showModal({
					title: '提示',
					showCancel: false,
					content: '当前人数访问过多，请稍后再试'
				});
				reject(xhr);
			};
			xhr.send(formData);
		})
	},
	post(url, data = {}, header = {}) {

		let options = {
			url: url,
			data: data,
			header: header,
			method: "POST"
		}

		return this.request(options);
	},

	get(url, data = {}, header = {}) {
		let options = {
			url: url,
			data: data,
			header: header
		}

		return this.request(options);
	}
};